package com.huatao.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.huatao.subject.application.convert.SubjectLikedConvert;
import com.huatao.subject.application.dto.SubjectLikedDTO;
import com.huatao.subject.common.entity.PageResult;
import com.huatao.subject.common.entity.Result;
import com.huatao.subject.common.utils.LoginUtil;
import com.huatao.subject.domain.entity.SubjectLikedBO;
import com.huatao.subject.domain.service.SubjectLikedDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/liked")
@Slf4j
public class SubjectLikedController {

    @Resource
    private SubjectLikedDomainService subjectLikedDomainService;

    @PostMapping("add")
    public Result<Boolean> addLiked(@RequestBody SubjectLikedDTO subjectLikedDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLikedController.addLiked.subjectLikedDTO: {}", JSON.toJSONString(subjectLikedDTO));
            }
            Preconditions.checkNotNull(subjectLikedDTO.getId(), "主键id不能为空");
            Preconditions.checkNotNull(subjectLikedDTO.getStatus(), "点赞状态不能为空");
            String loginId = LoginUtil.getLoginId();
            Preconditions.checkArgument(!StringUtils.isBlank(loginId), "登陆id不能为空");
            SubjectLikedBO subjectLikedBO = SubjectLikedConvert.INSTANCE.convertLikedDtoToBo(subjectLikedDTO);
            subjectLikedDomainService.addLiked(subjectLikedBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectLikedController.addLiked.error: {}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("update")
    public Result<Boolean> updateLiked(@RequestBody SubjectLikedDTO subjectLikedDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLikedController.addLiked.subjectLikedDTO: {}", JSON.toJSONString(subjectLikedDTO));
            }
            Preconditions.checkNotNull(subjectLikedDTO.getId(), "主键id不能为空");
            SubjectLikedBO subjectLikedBO = SubjectLikedConvert.INSTANCE.convertLikedDtoToBo(subjectLikedDTO);
            Boolean res = subjectLikedDomainService.updateLiked(subjectLikedBO);
            return Result.ok();
        } catch (Exception e) {
            log.error("SubjectLikedController.addLiked.error: {}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("delete")
    public Result<Boolean> deleteLiked(@RequestBody SubjectLikedDTO subjectLikedDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLikedController.addLiked.subjectLikedDTO: {}", JSON.toJSONString(subjectLikedDTO));
            }
            Preconditions.checkNotNull(subjectLikedDTO.getId(), "主键id不能为空");
            SubjectLikedBO subjectLikedBO = SubjectLikedConvert.INSTANCE.convertLikedDtoToBo(subjectLikedDTO);
            Boolean res = subjectLikedDomainService.deleteLiked(subjectLikedBO);
            return Result.ok();
        } catch (Exception e) {
            log.error("SubjectLikedController.addLiked.error: {}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("getSubjectLikedCount")
    public Result<PageResult<SubjectLikedDTO>> getSubjectLikedCount(@RequestBody SubjectLikedDTO subjectLikedDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLikedController.getSubjectLikedCount.SubjectLikedDTO: {}", JSON.toJSONString(subjectLikedDTO));
            }
            SubjectLikedBO subjectLikedBO = SubjectLikedConvert.INSTANCE.convertLikedDtoToBo(subjectLikedDTO);
            subjectLikedBO.setPageNo(subjectLikedDTO.getPageNo());
            subjectLikedBO.setPageSize(subjectLikedDTO.getPageSize());
            PageResult<SubjectLikedBO> boPageResult = subjectLikedDomainService.getSubjectLikedCount(subjectLikedBO);
            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectLikedController.getSubjectLikedCount.error: {}", e.getMessage(), e);
            return Result.fail("分页查询点赞失败");
        }
    }

}
